package main

import (
	"fmt"
	"longmen/server/pkg/common/lunar/calendar"
	"strings"
	"time"
)

type Bazi struct {
	YearGanZhi  string
	MonthGanZhi string
	DayGanZhi   string
	HourGanZhi  string
}

func main() {
	lunar := calendar.NewLunar(1988, 5, 23, 19, 0, 0)

	fmt.Println(lunar.ToFullString())

	//return
	//
	//birthDate := time.Date(1988, 7, 6, 19, 0, 0, 0, time.UTC) // 替换为用户的出生日期
	//
	//lunarDate := fmt.Sprintf("%d%d%d", birthDate.Year(), birthDate.Month(), birthDate.Day())
	//
	//err, lunar := server.Lunar(birthDate)
	//if err != nil {
	//	fmt.Println(err, lunarDate)
	//	return
	//}
	//fmt.Println(lunar)
	//
	//bazi := calculateBazi(lunar.Year, lunar.Month, lunar.Day, birthDate.Hour(), birthDate)
	//
	//fmt.Printf("八字：年:%s 月:%s 日:%s 时:%s\n", bazi.YearGanZhi, bazi.MonthGanZhi, bazi.DayGanZhi, bazi.HourGanZhi)
	//
	////fmt.Println(interpretBazi(bazi.YearGanZhi, bazi.MonthGanZhi, bazi.DayGanZhi, bazi.HourGanZhi))
	//
	//missingWuXing := findMissingWuXing(bazi.YearGanZhi + bazi.MonthGanZhi + bazi.DayGanZhi + bazi.HourGanZhi)
	//fmt.Printf("八字缺失的五行元素：%s\n", strings.Join(missingWuXing, "、"))
}

func calculateBazi(year, month, day, hour int, birthDate time.Time) Bazi {
	// 计算年柱
	yearGanZhi := calculateGanZhi(year)

	// 计算月柱
	monthGanZhi := calculateMonthGanZhi(5)

	// 计算日柱
	dayGanZhi := calculateGanZhi(6)

	// 计算时柱
	hourGanZhi := calculateHourGanZhi(hour)

	return Bazi{
		YearGanZhi:  yearGanZhi,
		MonthGanZhi: monthGanZhi,
		DayGanZhi:   dayGanZhi,
		HourGanZhi:  hourGanZhi,
	}
}

//天干地支
func calculateGanZhi(value int) string {
	// 天干的名称
	tianGan := []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}

	// 地支的名称
	diZhi := []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}

	// 计算天干的索引
	tianGanIndex := (value - 4) % 10
	if tianGanIndex < 0 {
		tianGanIndex += 10
	}

	// 计算地支的索引
	diZhiIndex := (value - 4) % 12
	if diZhiIndex < 0 {
		diZhiIndex += 12
	}

	// 返回结果
	return tianGan[tianGanIndex] + diZhi[diZhiIndex]
}

func calculateMonthGanZhi(month int) string {
	// 计算月柱
	return calculateGanZhi(month)
}

func calculateHourGanZhi(hour int) string {
	// 时辰的干支
	hourGanZhi := calculateGanZhi(hour/2 + 1)
	return hourGanZhi
}

//######################八字缺失
// 判断五行属性的函数
func findWuXing(element string) string {
	wuXingMap := map[string]string{
		"甲": "木", "乙": "木", "丙": "火", "丁": "火",
		"戊": "土", "己": "土", "庚": "金", "辛": "金",
		"壬": "水", "癸": "水",
	}
	return wuXingMap[element]
}

// 判断八字缺失的五行元素
func findMissingWuXing(bazi string) []string {
	elements := strings.Split(bazi, "")
	missingWuXing := make(map[string]bool)

	for _, element := range elements {
		if wuXing := findWuXing(element); wuXing != "" {
			missingWuXing[wuXing] = true
		}
	}

	allWuXing := []string{"木", "火", "土", "金", "水"}
	var result []string

	for _, wuXing := range allWuXing {
		if _, found := missingWuXing[wuXing]; !found {
			result = append(result, wuXing)
		}
	}

	return result
}
